package tool

/*******************************************************************************
Copyright:cloud
Author:cloudapex@126.com
Version:1.0
Date:2020-06-12
Description: 权限数据接口
*******************************************************************************/
import (
	"permissions/api/cache"
	"permissions/model"

	prds "gitee.com/cloudapex/permit/cache"
	"gitee.com/cloudapex/ulern/htp"
	"github.com/gin-gonic/gin"
)

/*
 * 描述: 用户的角色缓存数据
 * 路由: /tool/cache_role
 * 方法: GET
 *********************************************************************/
type CacheRole struct {
	UId   int64  `form:"uid" binding:"omitempty"`   // UId
	Name  string `form:"name" binding:"omitempty"`  // 名称
	Phone int64  `form:"phone" binding:"omitempty"` // 手机
}

func (this *CacheRole) Handle(c *gin.Context) htp.Response {
	if this.UId == 0 && this.Name == "" && this.Phone == 0 {
		return htp.RespParamErr("请至少使用一个查询条件", nil)
	}
	usr := model.User{Id: this.UId, Name: this.Name, Phone: this.Phone}
	if has, err := usr.Load(); !has || err != nil {
		return htp.RespErr(htp.CodeTargetNotFound, "目标不存在", nil)
	}

	role := prds.SUserRole{Id: usr.Id}
	if err := role.Load(); err != nil {
		return htp.RespErr(htp.CodePermitRdsRole, "CodePermitRdsRole", err)
	}
	return htp.RespOK("", &role)
}

/*
 * 描述: 路由的缓存数据
 * 路由: /tool/cache_route
 * 方法: GET
 *********************************************************************/
type CacheRoute struct {
	Route string `form:"route" binding:"required"` // 名称
}

func (this *CacheRoute) Handle(c *gin.Context) htp.Response {
	route := prds.HRouteRole{Route: this.Route}
	if err := route.Load(); err != nil {
		return htp.RespErr(htp.CodePermitRdsRoute, "CodePermitRdsRoute", err)
	}
	return htp.RespOK("", &route)
}

/*
 * 描述: 重置路由缓存数据
 * 路由: /tool/cache_reset
 * 方法: POST
 *********************************************************************/
type CacheReset struct {
}

func (this *CacheReset) Handle(c *gin.Context) htp.Response {
	if err := cache.ResetRouteRoles(); err != nil {
		return htp.RespErr(htp.CodeSysError, "系统错误", err)
	}
	return htp.RespOK("", nil)
}
